/*exported JSJaCKeys */

import { JSJAC_NKEYS } from "./JSJaCConfig";

/**
 * Creates a new set of hash keys
 * @class Reflects a set of sha1/md5 hash keys for securing sessions
 * @constructor
 * @param {Function} func The hash function to be used for creating the keys
 * @param {Debugger} oDbg Reference to debugger implementation [optional]
 */
export class JSJaCKeys {

  private _k:string[] = [];
  private oDbg:any;

  private _indexAt:number = JSJAC_NKEYS-1;
  constructor(func?:Function,oDbg?:{log:Function}){
    let seed = Math.random();
    this._k[0] = seed.toString();

    if(oDbg){
      this.oDbg=oDbg
    } else {
      this.oDbg = {};
      this.oDbg.log = function() {};
    }
    if(func) {
      for (var i=1; i<JSJAC_NKEYS; i++) {
        this._k[i] = func(this._k[i-1]);
        oDbg.log(i+": "+this._k[i],4);
      }
    }

  }
  /**
   * Gets next key from stack
   * @return New hash key
   * @type String
   */
  getKey():string {
    return this._k[this._indexAt--];
  };
  /**
   * Indicates whether there's only one key left
   * @return <code>true</code> if there's only one key left, false otherwise
   * @type boolean
   */
  lastKey():boolean{ return (this._indexAt === 0); };
  /**
   * Returns number of overall/initial stack size
   * @return Number of keys created
   * @type int
   */
  size():number { return this._k.length; };

  /**
   * @private
   */
  _getSuspendVars():string[] {
    return ('_k,_indexAt').split(',');
  };
}